package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.BatteryModel;
import com.btkjgc.models.extend.ExceptionRecordModel;
import com.btkjgc.models.extend.ExchangeElectricRecordModel;
import com.btkjgc.models.extend.PackagePostageDetailModel;
import com.btkjgc.service.intf.ElectricChargingRecordManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class ElectricChargingRecordManageController {

	static Logger logger = Logger.getLogger(ElectricChargingRecordManageController.class);

    @Autowired
	private ElectricChargingRecordManageService_Intf electricChargingRecordManageService;
    
    
	@RequestMapping(value = "electric_charging_record_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,ExchangeElectricRecordModel exchangeElectricRecordModel) {

		ModelAndView mv = new ModelAndView("electric_charging_record_manage");
		getElectricChargingRecordList(request, mv,exchangeElectricRecordModel);
		return mv;
	}


 
	@SuppressWarnings("unchecked")
	private void getElectricChargingRecordList(HttpServletRequest request, ModelAndView mv,ExchangeElectricRecordModel exchangeElectricRecordModel) {
 
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "tel","like", exchangeElectricRecordModel.getTel()};
		Object[] arr2 = { "carbrand","like", exchangeElectricRecordModel.getCarbrand() };
		Object[] arr3 = { "siteid","=", exchangeElectricRecordModel.getSiteid() };
		Object[] arr4 = { "companyid","=", exchangeElectricRecordModel.getCompanyid() };
		Object[] arr5 = { "cartypeid","=", exchangeElectricRecordModel.getCartypeid() };
		Object[] arr6 = { "createdate","d>", exchangeElectricRecordModel.getBegintime() };
		Object[] arr7 = { "createdate","d<", exchangeElectricRecordModel.getEndtime() };
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);
		paramList.add(arr5);
		paramList.add(arr6);
		paramList.add(arr7);
		

		String sql = "select * from (SELECT\n" +
					 "	eert.*, CONCAT(cartypet.name,CONCAT('-',cat.carbrand)) carbrand,\n" +
					 "	ait.available_balance availableBalance,\n" +
					 "	aut2. NAME companyname,\n" +
					 "	aut1. account driverId,\n" +
					 "	aut1.tel,\n" +
					 "   ppt.unitprice,\n" +
					 "	ecst. NAME sitename,\n" +
					 "	ct.companyid,\n" +
					 "   cat.cartypeid,\n" +
					 "	CASE eert.status\n" +
					 "WHEN 1 THEN\n" +
					 "	'正常'\n" +
					 "WHEN 2 THEN\n" +
					 "	'非正常'\n" +
					 "END AS statusName\n" +
					 "FROM\n" +
					 "	exchange_electric_record_t eert\n" +
					 "LEFT JOIN package_postage_t ppt ON eert.postageid = ppt.id\n" +
					 "AND ppt.del = 0\n" +
					 "LEFT JOIN car_account_log_t cat ON eert.carAccountid = cat.id\n" +
					 "LEFT JOIN car_t ct ON ct.id = cat.carid\n" +
					 "AND ct.del = 0\n" +
					 "LEFT JOIN cartype_t cartypet ON cartypet.id=ct.cartypeid\n"+
					 "AND cartypet.del = 0\n" +
					 "LEFT JOIN account_user_t aut1 ON aut1.id = cat.accountid\n" +
					 "AND aut1.del = 0\n" +
					 "LEFT JOIN account_info_t ait ON ait.accountid = aut1.id\n" +
					 "LEFT JOIN account_user_t aut2 ON aut2.id = ct.companyid\n" +
					 "AND aut2.del = 0\n" +
					 "LEFT JOIN electric_charging_site_t ecst ON ecst.id = eert.siteid\n" +
					 "AND ecst.del = 0\n" +
					 "WHERE\n" +
					 "	eert.del = 0) a where 1=1";

		sql = electricChargingRecordManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = electricChargingRecordManageService.getCount(sql, paramList);

		//获取分页查询的数据结果
		List<ExchangeElectricRecordModel> exchangeElectricRecordModelList = electricChargingRecordManageService.getList(
				ExchangeElectricRecordModel.class, paramList, sql, pageIndex, 15, sortfield);
		
		//为每一条记录设置应收计费里程数据
		for(int i = 0;i < exchangeElectricRecordModelList.size();i++){
			if(exchangeElectricRecordModelList.get(i).getPostageType()==2){//套餐计费
				String detailsql = " select * from package_postage_detail_t where conPostageid="+exchangeElectricRecordModelList.get(i).getPostageid();
				List<PackagePostageDetailModel> PackagePostageDetailModelList = HibernateUtil.executeSQLQueryList(detailsql, PackagePostageDetailModel.class);
				for(int j = 0;j < PackagePostageDetailModelList.size();j++){
					if(PackagePostageDetailModelList.get(j).getSort()==1){
						if(exchangeElectricRecordModelList.get(i).getTravelMileage() > PackagePostageDetailModelList.get(j).getLower()){
							if(PackagePostageDetailModelList.get(j).getMileage()!=null){
								exchangeElectricRecordModelList.get(i).setBillingmileage(PackagePostageDetailModelList.get(j).getMileage());
							}else{
								exchangeElectricRecordModelList.get(i).setBillingmileage(exchangeElectricRecordModelList.get(i).getTravelMileage());
							}
						}
					}else if(PackagePostageDetailModelList.get(j).getSort()==2){
						if(exchangeElectricRecordModelList.get(i).getTravelMileage() <= PackagePostageDetailModelList.get(j).getUpper()){
							if(PackagePostageDetailModelList.get(j).getMileage()!=null){
								exchangeElectricRecordModelList.get(i).setBillingmileage(PackagePostageDetailModelList.get(j).getMileage());
							}else{
								exchangeElectricRecordModelList.get(i).setBillingmileage(exchangeElectricRecordModelList.get(i).getTravelMileage());
							}
						}
					}else{
						if(exchangeElectricRecordModelList.get(i).getTravelMileage() <= PackagePostageDetailModelList.get(j).getUpper()&&exchangeElectricRecordModelList.get(i).getTravelMileage() > PackagePostageDetailModelList.get(j).getLower()){
							if(PackagePostageDetailModelList.get(j).getMileage()!=null){
								exchangeElectricRecordModelList.get(i).setBillingmileage(PackagePostageDetailModelList.get(j).getMileage());
							}else{
								exchangeElectricRecordModelList.get(i).setBillingmileage(exchangeElectricRecordModelList.get(i).getTravelMileage());
							}
						}
					}
				}
			}
		}

		//设置后台用户界面所需的其他数据
		electricChargingRecordManageService.setListData(request, mv, pageIndex, totalRecord,
				exchangeElectricRecordModelList);

	}
	
	@RequestMapping(value = "electric_charging_battery_list", method = { RequestMethod.GET,
			RequestMethod.POST })
	private ModelAndView getBatteryRecordList(HttpServletRequest request,BatteryModel batteryModel) {
		
		ModelAndView mv = new ModelAndView("electric_charging_battery_list");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		
		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "eebt.execreid","=",batteryModel.getExelecRecordid()};
		paramList.add(arr1);
		
		String sql ="SELECT\n" +
				"	eebt.*, CASE eebt.updown\n" +
				"WHEN 1 THEN\n" +
				"	'换上'\n" +
				"WHEN 2 THEN\n" +
				"	'换下'\n" +
				"END undownname\n" +
				"FROM\n" +
				"	exchange_electric_battery_t eebt where 1=1";

		sql = electricChargingRecordManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = electricChargingRecordManageService.getCount(sql, paramList);

		//获取分页查询的数据结果
		List<BatteryModel> batteryModelList = electricChargingRecordManageService.getList(
				BatteryModel.class, paramList, sql, pageIndex, 15, sortfield);

		//设置后台用户界面所需的其他数据
		electricChargingRecordManageService.setBatteryListData(request, mv, pageIndex, totalRecord,
				batteryModelList);
		
		return mv;
	}

	@RequestMapping(value = "download_exchange_record", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void downloadExchangeRecord(HttpServletRequest request,HttpServletResponse response) {
		
		String sql = "select * from (SELECT\n" +
				 	 "	eert.*, CONCAT(cartypet.name,CONCAT('-',cat.carbrand)) carbrand,\n" +
				 	 "	ait.available_balance availableBalance,\n" +
				 	 "	aut2. NAME companyname,\n" +
				 	 "	aut1. account driverId,\n" +
				 	 "	aut1.tel,\n" +
				 	 "   ppt.unitprice,\n" +
				 	 "	ecst. NAME sitename,\n" +
				 	 "	ct.companyid,\n" +
				 	 "   cat.cartypeid,\n" +
				 	 "	CASE eert.status\n" +
				 	 "WHEN 1 THEN\n" +
				 	 "	'正常'\n" +
				 	 "WHEN 2 THEN\n" +
				 	 "	'非正常'\n" +
				 	 "END AS statusName\n" +
				 	 "FROM\n" +
				 	 "	exchange_electric_record_t eert\n" +
				 	 "LEFT JOIN package_postage_t ppt ON eert.postageid = ppt.id\n" +
				 	 "AND ppt.del = 0\n" +
				 	 "LEFT JOIN car_account_log_t cat ON eert.carAccountid = cat.id\n" +
				 	 "LEFT JOIN car_t ct ON ct.id = cat.carid\n" +
				 	 "AND ct.del = 0\n" +
				 	 "LEFT JOIN cartype_t cartypet ON cartypet.id=ct.cartypeid\n"+
				 	 "AND cartypet.del = 0\n" +
				 	 "LEFT JOIN account_user_t aut1 ON aut1.id = cat.accountid\n" +
				 	 "AND aut1.del = 0\n" +
				 	 "LEFT JOIN account_info_t ait ON ait.accountid = aut1.id\n" +
				 	 "LEFT JOIN account_user_t aut2 ON aut2.id = ct.companyid\n" +
				 	 "AND aut2.del = 0\n" +
				 	 "LEFT JOIN electric_charging_site_t ecst ON ecst.id = eert.siteid\n" +
				 	 "AND ecst.del = 0\n" +
				 	 ") a where 1=1";
		
		electricChargingRecordManageService.doDownloadAction(request, response, sql);
		
	}
	
	@RequestMapping(value = "get_person_byexchageid")
	@ResponseBody
	public ExceptionRecordModel getPersonByExchageid(HttpServletRequest request,
			String id) {
		
		ExceptionRecordModel exceptionRecordModel = electricChargingRecordManageService.getPersonByExchageid(id);

	    return exceptionRecordModel;
	}
	
	@RequestMapping(value = "get_record_byexchageid")
	@ResponseBody
	public AjaxObj getRecordByExchageid(HttpServletRequest request,
			String id) {
		
		AjaxObj ajaxObj = electricChargingRecordManageService.getRecordByExchageid(id);
		
		return ajaxObj;
	}
	
	@RequestMapping(value = "add_refund_record/{a}")
	@ResponseBody
	public AjaxObj jsonAddRefundRecord(HttpServletRequest request,
			@PathVariable("a") int a ,ExceptionRecordModel exceptionRecordModel) {
		
		AjaxObj ajaxObj = electricChargingRecordManageService.insertRefundRecord(exceptionRecordModel);
		
		return ajaxObj;
	}

}
